package site.jlopen.kuscia.rpc.impl;

import java.io.FileNotFoundException;

import org.secretflow.v1alpha1.factory.KusciaAPIChannelFactory;
import org.secretflow.v1alpha1.kusciaapi.DomainDataSourceServiceGrpc;
import org.secretflow.v1alpha1.kusciaapi.DomainDataSourceServiceGrpc.DomainDataSourceServiceBlockingStub;
import org.secretflow.v1alpha1.kusciaapi.Domaindatasource.BatchQueryDomainDataSourceRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindatasource.BatchQueryDomainDataSourceResponse;
import org.secretflow.v1alpha1.kusciaapi.Domaindatasource.CreateDomainDataSourceRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindatasource.CreateDomainDataSourceResponse;
import org.secretflow.v1alpha1.kusciaapi.Domaindatasource.DeleteDomainDataSourceRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindatasource.DeleteDomainDataSourceResponse;
import org.secretflow.v1alpha1.kusciaapi.Domaindatasource.ListDomainDataSourceRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindatasource.ListDomainDataSourceResponse;
import org.secretflow.v1alpha1.kusciaapi.Domaindatasource.QueryDomainDataSourceRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindatasource.QueryDomainDataSourceResponse;
import org.secretflow.v1alpha1.kusciaapi.Domaindatasource.UpdateDomainDataSourceRequest;
import org.secretflow.v1alpha1.kusciaapi.Domaindatasource.UpdateDomainDataSourceResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.grpc.ManagedChannel;
import site.jlopen.kuscia.exception.SdkBaseException;
import site.jlopen.kuscia.properties.KusciaAPIProperties;
import site.jlopen.kuscia.rpc.KusciaDomainDataSourceRpc;

public class KusciaDomainDataSourceRpcImpl implements KusciaDomainDataSourceRpc {
	private static Logger logger = LoggerFactory.getLogger(KusciaDomainDataSourceRpcImpl.class);

	private DomainDataSourceServiceBlockingStub blockingStub;
	private ManagedChannel channel;

	public KusciaDomainDataSourceRpcImpl(KusciaAPIProperties properties) {
		try {
			KusciaAPIChannelFactory channelFactory = new KusciaAPIChannelFactory(properties.getAddress(),
					properties.getTokenFile(), properties.getTls(), properties.getProtocol());
			this.channel = channelFactory.newClientChannel();
			this.blockingStub = DomainDataSourceServiceGrpc.newBlockingStub(channel);
		} catch (Exception e) {
			if(e instanceof FileNotFoundException) {
				throw SdkBaseException.of("证书文件不存在", e);
			}
			throw SdkBaseException.of("Channel通讯异常", e);
		}
	}

	@Override
	public CreateDomainDataSourceResponse createDomainDataSource(CreateDomainDataSourceRequest request) {
		try {
			CreateDomainDataSourceResponse response = blockingStub.createDomainDataSource(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if (null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

	@Override
	public UpdateDomainDataSourceResponse updateDomainDataSource(UpdateDomainDataSourceRequest request) {
		try {
			UpdateDomainDataSourceResponse response = blockingStub.updateDomainDataSource(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if (null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

	@Override
	public DeleteDomainDataSourceResponse deleteDomainDataSource(DeleteDomainDataSourceRequest request) {
		try {
			DeleteDomainDataSourceResponse response = blockingStub.deleteDomainDataSource(request);
			//checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if (null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

	@Override
	public QueryDomainDataSourceResponse queryDomainDataSource(QueryDomainDataSourceRequest request) {
		try {
			QueryDomainDataSourceResponse response = blockingStub.queryDomainDataSource(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if (null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

	@Override
	public BatchQueryDomainDataSourceResponse batchQueryDomainDataSource(BatchQueryDomainDataSourceRequest request) {
		try {
			BatchQueryDomainDataSourceResponse response = blockingStub.batchQueryDomainDataSource(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if (null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

	@Override
	public ListDomainDataSourceResponse listDomainDataSource(ListDomainDataSourceRequest request) {
		try {
			ListDomainDataSourceResponse response = blockingStub.listDomainDataSource(request);
			checkResponse(response.getStatus());
			return response;
		} catch (Exception e) {
			logger.error("{}", e);
		} finally {
			if (null != channel) {
				channel.shutdown();
			}
		}
		return null;
	}

}
